package tos

import (
	"context"
	"tosServer/internal/pkg/common"

	"github.com/gin-gonic/gin"
	"github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

type renameParams struct {
	Ak       string `json:"ak" binding:"required"`
	Sk       string `json:"sk" binding:"required"`
	Endpoint string `json:"endpoint" binding:"required"`
	Region   string `json:"region" binding:"required"`
	Bucket   string `json:"bucket" binding:"required"`
	Key      string `json:"key" binding:"required"`
	NewKey   string `json:"new_key" binding:"required"`
}

/**
 * 重命名tos文件
 */
func (h *Object) Rename(c *gin.Context) {

	renameParams := &renameParams{}
	if err := c.ShouldBind(renameParams); err != nil {
		h.Api.Error(c, &common.R{
			Msg: err.Error(),
		})
		return
	}

	credential := tos.NewStaticCredentials(renameParams.Ak, renameParams.Sk)
	client, err := tos.NewClientV2(renameParams.Endpoint, tos.WithCredentials(credential), tos.WithRegion(renameParams.Region))
	if err != nil {
		h.Api.Error(c, &common.R{
			Msg: err.Error(),
		})
		return
	}
	defer client.Close()
	output, err := client.RenameObject(context.Background(), &tos.RenameObjectInput{
		Bucket: renameParams.Bucket,
		Key:    renameParams.Key,
		NewKey: renameParams.NewKey,
	})

	if err != nil {
		h.Api.Error(c, &common.R{
			Msg: err.Error(),
		})
		return
	}

	h.Api.Success(c, &common.R{
		Data: output,
	})
}
